package com.surelution.lansao

class PartnerConsoleController {
	
	def beforeInterceptor = {
		partner = springSecurityService.currentUser.partner
	}
	
	def springSecurityService
	def partner
	
    def index = { }
	
	def newOrders = {
		def status = OrderStatus.status(OrderStatus.NEW)
		def orders = Order.findAllByPartnerAndStatus(partner, status)
		[orders: orders]
	}
	
	def confirmedOrders = {
		def status = OrderStatus.status(OrderStatus.CONFIRMED)
		def orders = Order.findAllByPartnerAndStatus(partner, status)
		[orders: orders]
	}
	
	def deliveredOrders = {
		def status = OrderStatus.status(OrderStatus.DELIVERED)
		def orders = Order.findAllByPartnerAndStatus(partner, status)
		[orders: orders]
	}
	
	def finishedOrders = {
		def status = OrderStatus.status(OrderStatus.FINISHED)
		def orders = Order.findAllByPartnerAndStatus(partner, status)
		[orders: orders]
	}
	
	def cancelledOrders = {
		def status = OrderStatus.status(OrderStatus.CANCELLED)
		def orders = Order.findAllByPartnerAndStatus(partner, status)
		[orders: orders]
	}
	
	def processNewOrder = {
		def order = Order.get(params.int('orderId'))
		def type = params.handleType
		def status
		def message
		switch(type) {
			case "1":
				status = OrderStatus.status(OrderStatus.CONFIRMED)
				message = "接受"
				break
			case "2":
				status = OrderStatus.status(OrderStatus.CANCELLED)
				message = "地址不详"
				break
			case "3":
				status = OrderStatus.status(OrderStatus.CANCELLED)
				message = "超出送餐范围"
				break
			case "4":
				status = OrderStatus.status(OrderStatus.CANCELLED)
				message = "已售完"
				break
		}
		if(message && status) {
			if(partner && order && order.partner.id == partner.id) {
				order.status = status
				order.processMessage = message
				order.save(flush:true)
			}
		}
	}
	
}
